/*
 *    Copyright 2011 M. Lauer
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
(!window.stew) ? window.stew = {} : null;
if (!stew.faces) {
	stew.faces = function() {
		// private variables
		var _isDebug = (jsf.getProjectStage() == 'Development');
		var _parentSuffix = '_wrapper';
		// private functions
		var _debug = function(paramText) {
			if (_isDebug) {
				console.log(paramText);
			}
		};
		var _initWidget = function(paramId) {
			// if the widget was reloaded by ajax it needs to be reinitialized
			var widget = dijit.byId(paramId);
			var node_parent = dojo.byId(paramId + _parentSuffix);
			// if a component was reloaded then the widget id attribute is
			// missing on the node
			// so destroy the widget if it is already existing and parse the
			// node parent
			// to reinitialize the widget another time
			if ((widget != null) && (dojo.hasAttr(node, 'widgetid'))) {
				widget.destroyRecursive();
				dojo.parser.parse(node_parent);
			}
			// if there is no widget then it was loaded the first time, so
			// initialize it through parsing the parent node
			else if (widget == null) {
				dojo.parser.parse(node_parent);
			}

		};
		var _initEnhancedDataGrid = function(paramId, paramGridData,
				paramGridLayout, paramAllowDnd, paramAllowNestedSorting) {
			var widget = dijit.byId(paramId + '_grid');
			if (widget != null) {
				widget.destroyRecursive();
			}
			var gridstore = new dojo.data.ItemFileReadStore({
				data : paramGridData
			});
			var pluginConfig = {};
			if (paramAllowDnd)
				pluginConfig.dnd = true;
			if (paramAllowNestedSorting)
				pluginConfig.nestedSorting = true;
			var grid = new dojox.grid.EnhancedGrid({
				query : {},
				id : (paramId + '_grid'),
				autoHeight : true,
				store : gridstore,
				clientSort : true,
				rowSelector : '20px',
				structure : paramGridLayout,
				plugins : pluginConfig
			}, document.createElement('div'));
			// append the new grid to the specified div :
			dojo.byId(paramId + _parentSuffix).appendChild(grid.domNode);

			// Call startup, in order to render the grid:
			grid.startup();
			return grid;
		};
		var _functionStore = new Array();
		return {
			// public functions
			addMethod : function(paramKey, paramValue) {
				_functionStore[paramKey] = paramValue;
			},
			getMethod : function(paramKey) {
				_functionStore[paramKey];
			},
			// closure for Dialog component
			Dialog : function(paramId) {
				// constructor actions
				_debug('init Dialog');
				_initWidget(paramId);
				return {

				};
			},
			AccordionContainer: function(paramId,paramStyle,paramStyleClass)
			{
				_debug('init Accordion Container');
				_initWidget(paramId);
				return {
					
				};
			},
			TabContainer: function(paramId,paramStyle,paramStyleClass)
			{
				_debug('init Tab Container');
				_initWidget(paramId);
				return {
					
				};
			},
			// closure for EnhancedDataGrid component
			EnhancedDataGrid : function(paramId, paramGridData,
					paramGridLayout, paramAllowDnd, paramAllowNestedSorting) {
				_debug('init EnhancedDataGrid');
				_initEnhancedDataGrid(paramId, paramGridData, paramGridLayout,
						paramAllowDnd, paramAllowNestedSorting);
				return {

				};
			},
			// closure for FilteringSelect component
			FilteringSelect : function(paramId) {
				var _id = paramId;
				// the filtering select widget is not declared, so initializing
				// is a little bit different
				var _node = dojo.byId(paramId + _parentSuffix).children[0];
				var _origFunction = dojo.attr(_node, 'onchange');
				stew.faces.addMethod(paramId, _origFunction);
				var _create = function(paramIdVal) {
					new dijit.form.FilteringSelect({
						id : paramIdVal,
						name : paramIdVal,
						onChange : function(event) {
							stew.faces.getMethod(id);
						}
					}, paramIdVal);
				};
				var _widget = dijit.byId(_id);
				if ((_widget != null) && (dojo.hasAttr(_node, 'widgetid'))) {
					_widget.destroyRecursivly();
					_create(_node.id);
				} else if (_widget == null) {
					_create(_node.id);
				}
				_widget = dijit.byId(_id);
				_widget.onChange = _origFunction;
				return {};
			}
		};
	}();
}
